package cn.kgc.web.controller;

import cn.kgc.web.entity.Student;
import cn.kgc.web.service.StudentService;
import cn.kgc.web.utils.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/1/13
 * @description  springmvc
 * localhost:9090/student/add
 * localhost:9090/student/update
 * localhost:9090/student/delete
 * localhost:9090/student/list
 */
@RestController
@RequestMapping("student")
@CrossOrigin    //该接口允许跨域访问
public class StudentController {

    @Resource
    private StudentService studentService;

     //获取学生信息列表    /student/list
     @RequestMapping("list")
    public ResponseResult studentList(Integer size,Integer currentPage,String stuName,String tel){

         System.out.println("size = " + size + ", currentPage = " + currentPage + ", stuName = " + stuName + ", tel = " + tel);

         // 根据前端的参数 实现分页查询
         // select   *  from student    limit   (currentPage-1)*size , size  所有数据分页
         // select   *  from student  where stu_name = ?  and tel = ?   limit   (currentPage-1)*size , size   搜索数据分页
         // select   *  from student  where stu_name = ?   limit   (currentPage-1)*size , size   搜索数据分页
         // select   *  from student  where  tel = ?   limit   (currentPage-1)*size , size   搜索数据分页

         // 创建分页条件
         LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
         lambda.likeRight(StringUtils.isNotEmpty(stuName),Student::getStuName,stuName)
                 .eq(StringUtils.isNotEmpty(tel),Student::getTel,tel)
                 .orderByDesc(Student::getId);

         Page<Student> studentPage = new Page<>(currentPage, size);
         studentService.page(studentPage,lambda);
         // 获取分页数据
         List<Student> records = studentPage.getRecords();
         // 数据库中数据的总条数
         long total = studentPage.getTotal();
         HashMap<String, Object> result = new HashMap<>();
         result.put("records",records);
         result.put("total",total);
         return ResponseResult.success().data(result);
    }

    // 删除学生信息    /student/delete    [1]
    @RequestMapping("delete")
    public ResponseResult  deleteStudent(@RequestBody  Integer[] sIds){

        // 将数组中id字段的数据全部删除  [1,2,3]
        studentService.removeBatchByIds(Arrays.asList(sIds));
        return ResponseResult.success().message("删除成功");
    }

    //  修改学生信息   /student/update
    @RequestMapping("update")
    public ResponseResult  updateStudent(@RequestBody  Student student){

        studentService.updateById(student);
        return ResponseResult.success().message("更新成功");
    }

    // 添加学生信息    /student/add
    @RequestMapping("add")
    public ResponseResult  addStudent(@RequestBody  Student student){

        // 将前端传递的数据 持久化到数据库
        student.setCreateTime(new Date());
        studentService.save(student);
        return ResponseResult.success().message("添加成功");
    }

}
